package ar.republicagroup.gestion.view;

import ar.republicagroup.gestion.services.ChequeServices;
import ar.republicagroup.gestion.util.SpringUtils;
import java.awt.Font;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.PieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.RefineryUtilities;


public class PieChart1 extends JFrame {
    
    private static ChequeServices chequeServices = (ChequeServices)SpringUtils.getBean("chequeServices");
    
    
    public PieChart1(String title) {
        super(title);
        this.setIconImage(new ImageIcon(getClass().getResource("/ar/republicagroup/gestion/view/images/moneybox.png")).getImage());
        setContentPane(createDemoPanel());
        
    }

    /**
     * Creates a sample dataset.
     * 
     * @return A sample dataset.
     */
    private static PieDataset createDataset() {
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	String fechaHoy = sdf.format(new Date()); 
        System.out.println("fechaHoy: "+fechaHoy);
        
        List<Object[]> result = chequeServices.getSumaDeChequesPorClienteDelDia(fechaHoy, "%");
        DefaultPieDataset dataset = new DefaultPieDataset();
        
        for(Object[] row : result){
            dataset.setValue(""+row[0], Double.parseDouble(row[1].toString()));
        }

        return dataset;        
    }
    
    /**
     * Creates a chart.
     * 
     * @param dataset  the dataset.
     * 
     * @return A chart.
     */
    private static JFreeChart createChart(PieDataset dataset) {
        
        JFreeChart chart = ChartFactory.createPieChart(
            "Totales del día por Cliente",  // chart title
            dataset,             // data
            true,               // include legend
            true,
            false
        );

        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
        plot.setNoDataMessage("Aún no se han cargado cheques en el sistema");
        plot.setCircular(false);
        plot.setLabelGap(0.02);
        
        PieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator(
            "{0}: {1} ({2})", new DecimalFormat("$###,###.##"), new DecimalFormat("0%"));
        plot.setLabelGenerator(gen);
        
        return chart;
        
    }
    
    
    
    /**
     * Creates a panel for the demo (used by SuperDemo.java).
     * 
     * @return A panel.
     */
    public static JPanel createDemoPanel() {
        JFreeChart chart = createChart(createDataset());
        return new ChartPanel(chart);
    }
    
    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(String[] args) {

        PieChart1 demo = new PieChart1("Pie Chart Demo 1");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }

}